<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

/* @var $this yii\web\View */
/* @var $model common\models\Banner */
/* @var $form yii\widgets\ActiveForm */
?>
<?=Html::jsFile('/static/common/js/ajaxfileupload.js')?>
<div class="banner-form">

    <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'code')->dropDownList(\common\helpers\TStringHelper::getBannerCode(), ['prompt' => Yii::t('common', 'select...')]) ?>

    <?= $form->field($model, 'file')->textInput(['id' => 'litpic']) ?>
    <input type="file" name="Filedata" class="file_upload_attach" id="upload_child_attach" data-id="attach">

    <?= $form->field($model, 'url')->textInput(['maxlength' => true])->hint('填写则可以跳转', ['style' => 'color: #777']) ?>

    <?= $form->field($model, 'type')->dropDownList(\common\helpers\TStringHelper::getBannerType(), ['id' => 'type']) ?>

    <?= $form->field($model, 'status')->dropDownList(\common\helpers\TStringHelper::getStatusText()) ?>

    <?= $form->field($model, 'order')->textInput() ?>

    <?= $form->field($model, 'remark')->textarea() ?>

    <div class="form-group">
        <?= Html::submitButton($model->isNewRecord ? Yii::t('common', 'create') : Yii::t('common', 'update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
<script>
$(function () {
    //此处用了change事件，当选择好图片打开，关闭窗口时触发此事件
    $("#upload_child_attach").change(function (e) {
        var val = e.target.value;
        var point = val.lastIndexOf(".");
        var type = val.substr(point).toLowerCase();
        if (type != ".jpg" && type != ".jpeg" && type != ".png" && type != ".mp4") {
            alert('系统目前只支持.jpg、.jpeg、.png文件格式');
            return false;
        }
        $.ajaxFileUpload({
            //处理图片的脚本路径
            url: '<?=\yii\helpers\Url::toRoute(['common/upload-thumb', 'event' => 1, 'dir' => 'thumb', 'thumb' => '120,168'])?>',
            type: 'post',       //提交的方式
            secureuri: false,   //是否启用安全提交
            fileElementId: 'upload_child_attach',     //file控件ID
            dataType: 'json',  //服务器返回的数据类型
            success: function (data, status) {  //提交成功后自动执行的处理函数
                if (data.error) {
                    alert(data.msg);
                    return false;
                } else {
                    $("input[id='litpic']").val(data.fileUrl);
                }
            },
            error: function (data, status, e) {   //提交失败自动执行的处理函数
                alert(e);
            }
        })
    })

})
</script>